<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<%@ page import="org.news.entity.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>全部新闻</title>
<%
    //获取用户数据
     manager mg=(manager) session.getAttribute("user"); 
     Object a1=request.getParameter("quit");
     if("t".equals(a1) ||mg==null){  //点击退出或者未登录状态
    	 session.removeAttribute("user");
    	 //将mg置为空，否则报空指针异常
    	 mg=new manager(null,null,null);
     }
    //获取服务端全部数据
    List<news> news =(List<news>)request.getAttribute("ns");
    List<news> nsBySort=(List<news>)request.getAttribute("nsBySort");
    int pageNum=(int)request.getAttribute("pageNum");
    int totalPages=(int)request.getAttribute("totalPages");
    Object sort=request.getAttribute("sort");
    //判断list是否为空
    if(news==null){    	
    	news=nsBySort;
    }
%>
</head>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/allNews.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/seleAllNews.js"></script>
<script type="text/javascript">   
//选择页数
function selePage(page){
	var p=page;
	var sort="<%=sort%>";
	if(isEmpty(sort)){
		location.href="/NewsManager_SSM/newsServlet/seleByPage/"+p;
	}else{
		location.href="/NewsManager_SSM/newsServlet/seleNewsBySort?sort="+sort+"&page="+p;
	}	
}
//上一页和下一页
function upAndDownPage(act){
	var action=act;
	var sort="<%=sort%>";
	var pageNum="<%=pageNum%>";
	var totalPage="<%=totalPages%>";
	
	if(action=="up"){
		if(pageNum=="1"){
			alert("不能翻页咯");
			return false;
		}
		
		if(isEmpty(sort)){
			location.href="/NewsManager_SSM/newsServlet/seleByPage/"+<%=pageNum-1%>;
		}else{
			location.href="/NewsManager_SSM/newsServlet/seleNewsBySort?sort="+sort+"&page="+<%=pageNum-1%>;
		}	
	}else if(action=="down"){
		if(pageNum==totalPage){
			alert("不能翻页咯");
			return false;
		}
		if(isEmpty(sort)){
			location.href="/NewsManager_SSM/newsServlet/seleByPage/"+<%=pageNum+1%>;
		}else{
			location.href="/NewsManager_SSM/newsServlet/seleNewsBySort?sort="+sort+"&page="+<%=pageNum+1%>;
		}	
	}
}
//进行登录判断
$(function () {
    var uname="<%=mg.getUname()%>";
    var uid="<%=mg.getId()%>";
    //判断用户名是否为空
    if(!isEmpty(uname)){
    	$("#info1").html("<span>欢迎您！&nbsp"+uname+"<a href='/NewsManager_SSM/index.jsp?quit=t'> [退出]</a></span>");
    	$("#info2").text("");
    }
});
</script>

<body>
<!-- 页面头部   -->
<header id="header_div">
  <div class="container">
	<div class="logo left">
		<img src="${pageContext.request.contextPath}/img/logo2.jpg" width="700" height="90">
	</div>
	<ul class="right">
	    <li id="info1"><a href="${pageContext.request.contextPath}/view/login/login.jsp">登录？</a></li>
	    <li id="info2"><a href="${pageContext.request.contextPath}/view/login/addMg.jsp">马上注册</a></li>
		<li><a href="${pageContext.request.contextPath}/view/News/addNew.jsp">添加新闻</a></li>
		<li><a href="${pageContext.request.contextPath}/upAndDownServlet/allFiles">管理新闻文件</a></li>	
		<li><a href="${pageContext.request.contextPath}/index.jsp">返回主页</a></li>
	</ul>
  </div>
</header>
<!-- 大体部分 -->
<div id="table_div">
<table id="table">
    <tr>
       <th>编号</th>
       <th>标题</th>
       <th>类别</th>
       <th>上传时间</th>
       <th>操作</th>
    </tr>
    <% if(news!=null){ for(news n:news){%>
    <tr>
        <td width="5%"><%=n.getId() %></td>
        <td width="40%"><%=n.getTitle() %></td>
        <td width="8%"><%=n.getSort() %></td>
        <td width="15%"><%=n.getTime() %></td>
        <td width="15%">
            <button onclick="updateNew(<%=n.getId()%>)">查看详情</button>
            <button onclick="updateNew(<%=n.getId()%>)">修改</button>
            <button onclick="deleNewById(<%=n.getId()%>)">删除</button>
        </td>
    </tr>
    <%           }     } %>

</table>
<div id="but_div">
    <button onclick="upAndDownPage('up')">上一页</button>
    <%  for(int i=1;i<=totalPages;i++){  %>
    <button onclick="selePage(<%=i%>)" style="width:40px;height:26px"><%=i %></button>
    <%   } %>
    <button onclick="upAndDownPage('down')">下一页</button>
</div>
</div>
<!--页脚部分-->
<div id="footer">
	<div class="container">
		<p>
			<img src="img/i-f-logo.png"> <span>@2020 北京市xxxxxxx</span>
			 <a href="#">版权说明</a> <a href="#">版权说明</a> <a href="#">版权说明</a> 
			 <a href="#">版权说明</a> <a href="#">版权说明</a>
		</p>
	</div>
</div>
</body>
</html>